home *** CD-ROM | disk | FTP | other *** search
- Path: garden.csc.calpoly.edu!not-for-mail
- From: dstubbs@garden.csc.calpoly.edu (Dan Stubbs)
- Newsgroups: comp.lang.c
- Subject: Re: Simple Program Question
- Date: 26 Feb 1996 16:10:24 -0800
- Organization: Cal Poly, San Luis Obispo
- Message-ID: <4gti5g$d78@garden.csc.calpoly.edu>
- References: <4gsr9u$sk6@newsbf02.news.aol.com>
- NNTP-Posting-User: dstubbs@garden.csc.calpoly.edu
-
- In article <4gsr9u$sk6@newsbf02.news.aol.com>, Tycope <tycope@aol.com> wrote:
- >I am trying to write a non -interactive program that calculates all
- >integer triples (i, j, k) such that
- >0 < i < j < k < l and i + j + k = l. Print out the number of triples that
- >satisfy the requirements and print out every millionth triple. Can anyone
- >see where I am missing the boat in the following code. The program runs
- >and immediately terminates. Thanks in advance for any feedback.
- >
- >#include <stdio.h>
- >
- >long int i, j, k, l;
- >long int count;
- >
- >int
- >main (void)
- >{
- > do
- > {
- > (l = i + j + k);
- > (i = 1);
- > (j = 2);
- > (k = 3);
- > (i++, j++, k++);
- > (++count);
- > if (count % 1000000 == 0)
- > {
- > printf("%ld(i) + %ld(j) + %ld(k) = %ld(l)", i, j, k, l);
- > }
- >
- > } while (0 < i < j < k < l);
- >
- > return (0);
- >}
-
- It appears to me that you are to fix el (use el instead of l) and then find
- i, j, and k such that 0<i<j<k and i+j+k = el. Your code doesn't do anything
- like that. A couple of problems: the first time l = i+j+k is encountered
- i, j, and k have had no value assigned and hence what happens is undefined.
- Perhaps that is where your program dies.
-
- Then, each time through your do-while loop i,j and k are *all* incremented by
- exactly 1. Hence the value of i,j and k in ;your program are 1,2,3; 2,3,4;
- 3,4,5; 4,5,6; ... j,j+1,j+2, ... . Surely you want something more general
- than that.
-
- I don't believe your code could compile. Your while condition:
-
- while (0<i<j<k<l) is not legal. I suspect you want something like:
-
- while (0<i && i<j && j<k && k<l).
-
- Your logic is pretty far off, so here is a solution to the problem as I
- understand it. (The fixed value of el is read from the command line.)
-
- /*----------------------------------------------*/
- #include <stdio.h>
-
- int main (int argc, char *argv[]) {
-
- int el = atoi(argv[1]);
- int i,j,k;
- int count = 0;
-
- for (i=1; i <= (el-3)/3; i++) {
- for (j = i+1; j <= (el - (i+1))/2; j++) {
- k = el - (i+j);
- count++;
- if (count % 1000000 == 0)
- printf ("%d+%d+%d = %d\n", i,j,k,el);
- }
- }
- printf (" count = %d\n", count);
- }
- /*----------------------------------------------*/
-
-